﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ISC.RIM.Silverlight;
using ISC.RIM.Silverlight.MapViewElement;
using OI.Controls;
using Point = System.Windows.Point;
using Polygon = ISC.MapDotNetServer.Common.Polygon;
using Polyline = ISC.MapDotNetServer.Common.Polyline;
using Shape = ISC.MapDotNetServer.Common.Shape;

namespace OI.Framework
{
    public class MapShapes
    {
        public static SolidColorBrush FillBrush
        {
            get
            {
                if (_FillBrush == null)
                {
                    NextDrawingColor();
                }
                return _FillBrush;
            }
            set
            {
                _FillBrush = value;
            }
        }
        private static SolidColorBrush _FillBrush = null;

        public static SolidColorBrush OutlineBrush
        {
            get
            {
                if (_OutlineBrush == null)
                {
                    NextDrawingColor();
                }
                return _OutlineBrush;
            }
            set
            {
                _OutlineBrush = value;
            }
        }
        private static SolidColorBrush _OutlineBrush = null;

      

        public static IMapViewChild CreateHighlightShape(Shape shape)
        {
            IMapViewChild shapeHighlight = null;

            if (shape is Polygon || shape is Polyline)
            {
                shapeHighlight = new PolyElement(
                    shape,
                    Application.Current.Resources["FeatureHighlightFillColorBrush"] as SolidColorBrush,
                    Application.Current.Resources["FeatureHighlightOutlineColorBrush"] as SolidColorBrush,
                    Configuration.ShapeOutlineThickness,
                    null);

                // attach pulsing animation
                var uc = (UserControl)shapeHighlight;
                var sb = new Storyboard {RepeatBehavior = RepeatBehavior.Forever, AutoReverse = true};
                var da = new DoubleAnimation {From = 0.1, To = 0.8, Duration = TimeSpan.FromMilliseconds(500)};

                var ease = new SineEase{ EasingMode = EasingMode.EaseInOut };
                da.EasingFunction = ease;
                
                Storyboard.SetTarget(da, uc);
                Storyboard.SetTargetProperty(da, new PropertyPath("Opacity"));
                sb.Children.Add(da);
                sb.Begin(); 

            }
            else if (shape is ISC.MapDotNetServer.Common.Point)
            {
                Point anchor = ((ISC.MapDotNetServer.Common.Point)shape).ToSysWinPoint();

                shapeHighlight = new PointElement(
                    anchor,
                    new DrillDownIcon());
            }
            return shapeHighlight;
        }


        public static IMapViewChild CreateDrawnShape(Shape shape, Brush fill, Brush outline)
        {
            return new PolyElement(
                    shape,
                    fill,
                    outline,
                    Configuration.ShapeOutlineThickness,
                    null);
        }


        public static IMapViewChild CreateDrawPoint(Point pt, Brush fill, Brush outline)
        {
            var ellip = new Ellipse
            {
                Stroke = outline,
                Fill = fill,
                StrokeThickness = 1.5,
                Width = Configuration.PointMarkerSize,
                Height = Configuration.PointMarkerSize
            };

            return new PointElement(pt, ellip);
        }


        private static readonly Color[] DrawColors = new []
        {
            Colors.Blue,
            Colors.Purple,
            Colors.Red,
            Colors.Orange,
            Colors.Yellow,
            Colors.Green,
            Colors.White,
            Colors.Gray,
            Colors.Black,
        };


        public static void NextDrawingColor()
        {
            _colorIdx++;
            if (_colorIdx >= DrawColors.Length)
            {
                _colorIdx = 0;
            }

            Color c = DrawColors[_colorIdx];
            OutlineBrush = new SolidColorBrush(c);
            c.A = 64;
            FillBrush = new SolidColorBrush(c);
        }
        private static int _colorIdx = -1;
    }
}
